//
//  HMEmoticonPageViewCell.swift
//  WB25
//
//  Created by HM on 2017/7/10.
//  Copyright © 2017年 itheima. All rights reserved.
//

import UIKit

class HMEmoticonPageViewCell: UICollectionViewCell {
    
    
    var indexPath: IndexPath?{
        didSet{
            
            messageLabel.text = "第\(indexPath?.section ?? 0)组\n第\(indexPath?.item ?? 0)页"
        }
            
    }
    // 提供属性 供外界赋值
    var emoticons:[HMEmoticonModel]?{
        didSet{
            
            // 先隐藏
            for button in buttonsList{
                button.isHidden = true
            }
            
            // 遍历emoticons
            for (i, emoticonModel) in emoticons!.enumerated(){
                
                // 通过下标获取按钮
                let button = buttonsList[i]
                
                // 赋值
                button.emoticonModel = emoticonModel
                
                // 显示按钮
                button.isHidden = false
                // 判断是否是emoji表情
                if emoticonModel.isEmoji {
                    // 如果是emoji表情 设置的为title
                    // 获取到code
                    let code = ((emoticonModel.code ?? "") as NSString).emoji()
                    
                    // 设置title
                    button.setTitle(code, for: UIControlState.normal)
                    button.setImage(nil, for: UIControlState.normal)
                }else{
                    // 如果是图片表情 设置的为image
                    let png = emoticonModel.allPath ?? ""
       
                    let image = UIImage(named: png, in: HMEmoticonTools.share.emoticonBundle, compatibleWith: nil)
                    button.setImage(image, for: UIControlState.normal)
                    button.setTitle(nil, for: UIControlState.normal)
                }
            }
           
        }
    }
    
    // 保存20个表情按钮的数组
    lazy var buttonsList: [HMEmoticonButton] = [HMEmoticonButton]()
    
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func layoutSubviews() {
        super.layoutSubviews()
        // 计算按钮的宽度和高度
        let buttonW = (self.frame.width - 10) / CGFloat(HMEMOTICONMAXCOL)
        let buttonH = (self.frame.height - 20) / CGFloat(HMEMOTICONMAXROW)
        // 遍历保存按钮的数组
        for (i, button) in buttonsList.enumerated(){
            // 行索引和列索引
            let rowIndex = i / HMEMOTICONMAXCOL
            let colIndex = i % HMEMOTICONMAXCOL
            button.frame = CGRect(x: CGFloat(colIndex)*buttonW + 5, y: CGFloat(rowIndex)*buttonH, width: buttonW, height: buttonH)
        }
        
        // 设置删除按钮的frame
        deleteButton.frame = CGRect(x: self.frame.width - 5 - buttonW, y: buttonH*2, width: buttonW, height: buttonH)
    }
    
    //MARK: - 设置视图
    private func setupUI(){
//        backgroundColor = randomColor()
        addChildButtons()
        addSubview(deleteButton)
        contentView.addSubview(messageLabel)
        
        messageLabel.snp.makeConstraints { (make) in
            make.center.equalTo(contentView)
        }
    }
    
    
    //MARK: - 懒加载控件
    private lazy var messageLabel: UILabel = {
        let lab = UILabel()
        lab.textColor =  UIColor.black
        lab.font = UIFont.systemFont(ofSize: 32)
        lab.numberOfLines = 0
        return lab
    }()
    // 删除按钮
    private lazy var deleteButton: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(deleteButtonClick), for: UIControlEvents.touchUpInside)
        button.setImage(UIImage(named:"compose_emotion_delete"), for: UIControlState.normal)
        button.setImage(UIImage(named:"compose_emotion_delete_highlighted"), for: UIControlState.highlighted)
        return button
    }()
}
//MARK: - 创建20个按钮
extension HMEmoticonPageViewCell{
    fileprivate func addChildButtons(){
        
        // 循环
        for _ in 0..<HMEMOTICONMAXCOUNT{
            // 创建button
            let button = HMEmoticonButton()
            button.addTarget(self, action: #selector(buttonClick), for: UIControlEvents.touchUpInside)
            button.titleLabel?.font = UIFont.systemFont(ofSize: 30)
//            button.backgroundColor = randomColor()
            buttonsList.append(button)
            contentView.addSubview(button)
        }
    }
}

extension HMEmoticonPageViewCell{
    
    @objc fileprivate func buttonClick(button: HMEmoticonButton){
        // 发送通知
        NotificationCenter.default.post(name: HMEMOTICONBUTTONCLICKNOTI, object: button.emoticonModel)
    }
    
    @objc fileprivate func deleteButtonClick(){
        // 发送通知
        NotificationCenter.default.post(name: HMDELETEBUTTONCLICKNOTI, object: nil)
    }
}
